<?php

namespace App\Http\Controllers\home;

use App\Http\Model\Goods;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cookie;
use Illuminate\Support\Facades\View;

//前台基类
class BaseController extends Controller
{
	public function __construct(Request $request)
	{
		//开启session
		session_start();
		date_default_timezone_set('PRC');
		$key  = "qaJNS12KS@ASu7r5";
		$user = Cookie::get('user');
		$id = Cookie::get('id');
		$user_info = Cookie::get('user_info');
		$cookiekey = md5($user.'|'.$id.'|'.$key);
		if($cookiekey !== $user_info )
		{
			setcookie('user',null);
			setcookie('id',null);
			setcookie('user_info',null);
		}
		//获取商品浏览记录
		$his = $this -> getHis();
		View::share('his',$his);
	}

	/**
	 * 获取商品浏览历史
	 */
	public function getHis(){
		if(!isset($_SESSION['his'])){
			return array();
		}else{
			$data = $_SESSION['his'];
		}
		$temp = [];
		foreach($data as $id){
			$d = Goods::find($id);
			if($d!=null){
				$temp[] = $d;
			}
		}
		return $temp;
	}
}
